package org.mm.fastdfs.fastdfs.service;

import lombok.extern.slf4j.Slf4j;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class FastDfsService {

    public static String conf_filename = "src/main/resources/fdfs_client.conf";

    static {
        try {
            ClientGlobal.init(conf_filename);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public String[] uploadFile(String filePath,String fileExt,NameValuePair[] nvps){
        TrackerServer trackerServer = null;
        try {
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            return storageClient.upload_file(filePath, fileExt, nvps);
        }catch (Exception e){
           log.info(e.getMessage());
        }finally{
            try {
                if(null!=trackerServer){
                    trackerServer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    public byte[] downloadFile(String groupName,String remotePath){
        TrackerServer trackerServer = null;
        try {
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            return storageClient.download_file(groupName, remotePath);
        }catch (Exception e){
            log.info(e.getMessage());
        }finally{
            try {
                if(null!=trackerServer){
                    trackerServer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public FileInfo getFileInfo(String groupName,String remotePath){
        TrackerServer trackerServer =null;
        try {
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer, null);
            return storageClient.get_file_info(groupName, remotePath);
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally{
            try {
                if(null!=trackerServer){
                    trackerServer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public NameValuePair[] getFileMate(String groupName,String remotePath){
        TrackerServer trackerServer =null;
        try {
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer,null);
            //这个值是上传的时候指定的NameValuePair
            return storageClient.get_metadata(groupName, remotePath);
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally{
            try {
                if(null!=trackerServer){
                    trackerServer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public Boolean deleteFile(String groupName,String remotePath){
        TrackerServer trackerServer =null;
        try {
            TrackerClient tracker = new TrackerClient();
            trackerServer = tracker.getConnection();
            StorageClient storageClient = new StorageClient(trackerServer,null);
            int i = storageClient.delete_file(groupName, remotePath);
            return (i==0 ? true:false);
        } catch (Exception e) {
            log.info(e.getMessage());
        } finally{
            try {
                if(null!=trackerServer){
                    trackerServer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
